#include "bdyqlineedit.h"
#include <QMouseEvent>
#include <QDebug>

BdyQLineEdit::BdyQLineEdit(QWidget *parent) : QLineEdit(parent)
{
    installEventFilter(this);
}

void BdyQLineEdit::save()
{
    // 保存回显模式
    defaultEchoMode_ = echoMode();
}
void BdyQLineEdit::error(QString && message)
{
    save();
    setEchoMode(QLineEdit::Normal);
    setStyleSheet("color:red");
    setText(message);
    removeEventFilter(this);
}

bool BdyQLineEdit::eventFilter(QObject *watched, QEvent *event)
{

    if(event->type() == QEvent::MouseButtonPress){
       QMouseEvent *e = static_cast<QMouseEvent *>(event);
       if(e->buttons() == Qt::LeftButton){
           return true;
       }
    }

    return QLineEdit::eventFilter(watched, event);
}

void BdyQLineEdit::mousePressEvent(QMouseEvent *e)
{
    if(e->buttons() == Qt::LeftButton){
        // 重新设置文本颜色以及回显模式并清空输入框
        setEchoMode(defaultEchoMode_);
        setStyleSheet("color:black");
        clear();
        installEventFilter(this);
    }

}

